#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("测试标题");
    this->setWindowIcon(QIcon(":/new1/Image/favicon.ico"));
}

Widget::~Widget()
{
    delete ui;
}

/*===========================<2-1内容关于文件打开的操作>===========================*/
void Widget::on_btn_openFile_clicked()
{

    QString fileDialogCaptionName("测试打开文件");
    QString defaultPath("C:/");

    qDebug() << defaultPath;

    QString myFilter("文本文件(*.txt);;图片文件(*.jpg *.gif);;所有文件(*.*)");
    QString fileName = QFileDialog::getOpenFileName(this,fileDialogCaptionName,defaultPath,myFilter);

    if(!fileName.isEmpty())
        ui->textEdit_fileName->setText(fileName);
    else
        ui->textEdit_fileName->setText("打开文件失败");
}


void Widget::on_btn_openMultipleFile_clicked()
{
    QString fileDialogCaptionName("测试打开多个文件");
    QString defaultPath = QDir::currentPath();

    qDebug() << defaultPath;

    QString myFilter("文本文件(*.txt);;图片文件(*.jpg *.gif);;所有文件(*.*)");
    QStringList fileNames = QFileDialog::getOpenFileNames(this, fileDialogCaptionName,defaultPath,myFilter);

    ui->textEdit_fileName->clear();
    if(!fileNames.isEmpty())
        for(int i =0; i < fileNames.size(); i++)
            ui->textEdit_fileName->append(fileNames.at(i));
    else
        ui->textEdit_fileName->append("打开多个文件失败");

}


void Widget::on_btn_openDir_clicked()
{
    QString fileDialogCaptionName("测试打开目录");
    QString defaultPath = QDir::currentPath();

    QString dirName = QFileDialog::getExistingDirectory(this,fileDialogCaptionName,defaultPath,QFileDialog::HideNameFilterDetails);

    if(!dirName.isEmpty())
        ui->textEdit_fileName->setText(dirName);
    else
        ui->textEdit_fileName->setText("打开文件失败");
}


void Widget::on_btn_saveFile_clicked()
{
    QString fileDialogCaptionName("测试保存文件");
    QString defaultPath = QDir::currentPath();
    QString myFilter("h文件(*.h);;C++文件(*.cpp);;所有文件(*.*)");

    QString fileName = QFileDialog::getSaveFileName(this,fileDialogCaptionName,defaultPath,myFilter);

    if(!fileName.isEmpty())
        ui->textEdit_fileName->setText(fileName);
    else
        ui->textEdit_fileName->setText("打开文件失败");
}
/*===========================<2-2内容关于字体颜色的操作>===========================*/

void Widget::on_btn_set_textColor_clicked()
{
    QPalette myPalatte = ui->textEdit_fileName->palette();

    QColor oldColor = myPalatte.color(QPalette::Text);

    QColor newColor = QColorDialog::getColor(oldColor,this,"测试设置字体颜色");

    if(newColor.isValid())
    {
        myPalatte.setColor(QPalette::Text,newColor);
        ui->textEdit_fileName->setPalette(myPalatte);
        // ui->textEdit_fileName->setTextColor(newColor);//似乎不行
    }
    else
        qDebug() << "字体颜色设置失败";
}


void Widget::on_btn_set_font_clicked()
{
    QFont oldFont = ui->textEdit_fileName->font();
    bool ok = false;
#if 1
    QFont newFont = QFontDialog::getFont(&ok,oldFont,this,"测试设置字体");
#else
    QFont newFont = QFontDialog::getFont(&ok);
#endif
    if(ok)
        ui->textEdit_fileName->setFont(newFont);
    else
        qDebug() << "字体设置失败";
}

